package com.bamboo.statistics.service.impl;

import com.bamboo.statistics.entity.Daily;
import com.bamboo.statistics.feign.UCenterFeignClient;
import com.bamboo.statistics.mapper.DailyMapper;
import com.bamboo.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author jack.wu
 * @since 2020-07-22
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Resource
    private UCenterFeignClient centerFeignClient;

    @Override
    public void getRegisterCount(String day) {
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date_calculated", day);
        baseMapper.delete(queryWrapper);
        int registerCount = centerFeignClient.getRegisterCount(day);
        Daily daily = new Daily();
        daily.setRegisterNum(registerCount);
        // todo
        daily.setLoginNum(registerCount);
        daily.setCourseNum(registerCount);
        daily.setDateCalculated(day);
        baseMapper.insert(daily);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        // 根据条件查询对应数据
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<Daily> staList = baseMapper.selectList(wrapper);

        // 因为返回有两部分数据：日期 和 日期对应数量
        List<String> calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        // 遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i++) {
            Daily daily = staList.get(i);
            // 封装日期list集合
            calculatedList.add(daily.getDateCalculated());
            // 封装对应数量
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        // 把封装之后两个list集合放到map集合，进行返回
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", calculatedList);
        map.put("numDataList", numDataList);
        return map;
    }
}
